import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/register',
      component: () => import('@/views/Register.vue'),
      meta: {
        title: '注册'
      }
    },
    {
      path: '/',
      redirect: '/noteclass'
    },
    {
      path: '/noteclass',
      component: () => import('@/views/NoteClass.vue'),
      meta: {
        title: '笔记分类'
      },
    },
    {
      path: '/notelist',
      component: () => import('@/views/NoteList.vue'),
      meta: {
        title: '笔记列表'
      },
    },
    {
      path:'/notedetail',
      component: () => import('@/views/NoteDetail.vue'),
      meta: {
        title: '笔记详情'
      },
    },
    {
      path:'/notepublish',
      component: () => import('@/views/NotePublish.vue'),
      meta: {
        title: '笔记修改'
      },
    }
  ]
})

// 全局的路由守卫
const whitePath = ['/login', '/register', '/noteClass']

router.beforeEach((to, from, next) => {
  document.title = to.meta.title
  // console.log(to);
  if (!whitePath.includes(to.path)) { // 登录
    // 判断浏览器本地有无userInfo
    if (!localStorage.getItem('userInfo')) { // 没登录
      router.push('/login')
      return 
    } 
    next()
    return
  } 
  next()
})
export default router
